/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.wxApi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.ruralLibrary.api.entity.ResourceEntity;
import com.pig4cloud.pigx.ruralLibrary.api.vo.ResourceVO;
import com.pig4cloud.pigx.wxApi.entity.CollectEntity;
import com.pig4cloud.pigx.wxApi.entity.SysUserInfo;
import com.pig4cloud.pigx.wxApi.mapper.CollectMapper;
import com.pig4cloud.pigx.wxApi.service.CollectService;
import com.pig4cloud.pigx.wxApi.service.ResourceService;
import com.pig4cloud.pigx.wxApi.service.UserInfoService;
import com.pig4cloud.pigx.wxApi.util.WxUntill;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 *
 * @author pig
 * @date 2023-11-15 14:19:29
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, CollectEntity> implements CollectService {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserInfoService userInfoSerice;
    @Override
    public R getUserResource(String openId, Long dataTypeId) {
        List<ResourceVO> resourceEntities = new ArrayList<>();
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CollectEntity::getOpenId, openId);
        wrapper.eq(CollectEntity::getDataTypeId, dataTypeId);
        List<CollectEntity> list1 = this.list(wrapper);
        if (list1.size() <= 0) {
          return R.ok();
        }
        for (CollectEntity collectEntity : list1) {
            ResourceEntity byId = resourceService.getById(collectEntity.getResourceId());
            if (byId == null){
                continue;
            }
            ResourceVO vo = new ResourceVO();
            BeanUtils.copyProperties(byId, vo);
            vo.setCollectId(collectEntity.getId());
            resourceEntities.add(vo);
        }

        return R.ok(resourceEntities);
    }

    public R getOpenId(String code){
        try {
            String wxOpenid = WxUntill.getWxOpenid(code);
            LambdaQueryWrapper<SysUserInfo> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(SysUserInfo::getOpenId,wxOpenid);
            SysUserInfo one = userInfoSerice.getOne(wrapper);
            if (one==null){
                return R.ok();
            }else {
                return R.ok(one);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getCollect(String openId, Long resourceId) {
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CollectEntity::getOpenId, openId);
        wrapper.eq(CollectEntity::getResourceId, resourceId);
        List<CollectEntity> list = this.list(wrapper);
        if (list.isEmpty()){
            return "0";
        }else {
            return "1";
        }
    }
}
